package com.ruoyi.kong.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.kong.controller.vo.GroupAddUsersVo;
import com.ruoyi.kong.domain.KSbh;
import com.ruoyi.kong.service.IKSbhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备管理Controller
 * 
 * @author zeroUp
 * @date 2024-06-19
 */
@RestController
@RequestMapping("/kong/sbh")
public class KSbhController extends BaseController
{
    @Autowired
    private IKSbhService kSbhService;

    /**
     * 查询设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('kong:sbh:list')")
    @GetMapping("/list")
    public TableDataInfo list(KSbh kSbh)
    {
        startPage();
        kSbh.setCreateBy(SecurityUtils.getUserId().toString());
        List<KSbh> list = kSbhService.selectKSbhList(kSbh);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('kong:sbh:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KSbh kSbh)
    {
        List<KSbh> list = kSbhService.selectKSbhList(kSbh);
        kSbh.setCreateBy(SecurityUtils.getUserId().toString());
        ExcelUtil<KSbh> util = new ExcelUtil<KSbh>(KSbh.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('kong:sbh:query')")
    @GetMapping(value = "/{gid}")
    public AjaxResult getInfo(@PathVariable("gid") Long gid)
    {
        return success(kSbhService.selectKSbhByGid(gid));
    }

    /**
     * 新增设备管理
     */
    @PreAuthorize("@ss.hasPermi('kong:sbh:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KSbh kSbh)
    {
        kSbh.setCreateBy(SecurityUtils.getUserId().toString());
        return toAjax(kSbhService.insertKSbh(kSbh));
    }

    /**
     * 修改设备管理
     */
    @PreAuthorize("@ss.hasPermi('kong:sbh:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KSbh kSbh)
    {
        return toAjax(kSbhService.updateKSbh(kSbh));
    }

    /**
     * 删除设备管理
     */
    @PreAuthorize("@ss.hasPermi('kong:sbh:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gids}")
    public AjaxResult remove(@PathVariable Long[] gids)
    {
        return toAjax(kSbhService.deleteKSbhByGids(gids));
    }


    @PostMapping("groupAddUsers")
    public AjaxResult groupAddUsers(@RequestBody GroupAddUsersVo vo)
    {

        return toAjax(kSbhService.updateKSbhByIds(vo));
    }

    @PostMapping("delgroupAddUsers")
    public AjaxResult delgroupAddUsers(@RequestBody GroupAddUsersVo vo)
    {
        vo.setGroup(null);
        return toAjax(kSbhService.updateKSbhByIds(vo));
    }
}
